package com.sais.manage.boot.building.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.building.dto.BuildingDTO;
import com.sais.manage.boot.building.dto.BuildingSaveDTO;
import com.sais.manage.boot.building.dto.BuildingSearchDTO;
import com.sais.manage.boot.building.dto.BuildingUser;
import com.sais.manage.boot.building.service.IBuildingService;
import com.sais.manage.boot.platform.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 宿舍管理系统-宿舍楼 controller 类
 *
 * @author zwp
 * @date 2020/1/30
 **/
@Api(tags = "宿舍楼管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/distribution/building")
public class BuildingController {

    @Resource
    private IBuildingService buildingService;

    @ApiOperation("宿舍楼 保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveBuilding(@RequestBody BuildingSaveDTO buildingSaveDTO) {
        buildingService.saveBuilding(buildingSaveDTO);
        return new Result();
    }

    @ApiOperation("宿舍楼 删除")
    @RequestMapping(value = "/delete/{buildingId}", method = RequestMethod.GET)
    public Result deleteBuilding(@PathVariable("buildingId") String buildingId) {
        buildingService.deleteBuilding(buildingId);
        return new Result();
    }

    @ApiOperation("根据id 查询")
    @RequestMapping(value = "/select/{buildingId}", method = RequestMethod.GET)
    public Result selectBuilding(@PathVariable("buildingId") String buildingId) {
        BuildingDTO building = buildingService.selectBuilding(buildingId);
        return new Result().data("building", building);
    }

    @ApiOperation("分页条件 查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listBuilding(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "30") Integer size,
                               BuildingSearchDTO searchDTO) {
        Page<BuildingDTO> page = buildingService.listBuilding(current, size, searchDTO);
        return new Result().data("buildings", page);
    }

    @ApiOperation("查询 房间类型列表")
    @RequestMapping(value = "/list/type/{gender}", method = RequestMethod.GET)
    public Result listBuildingType(@PathVariable(value = "gender") Integer gender) {
        List<String> types = buildingService.listBuildingType(gender);
        return new Result().data("types", types);
    }

    @ApiOperation("宿舍楼 绑定工作人员")
    @RequestMapping(value = "/bind/user", method = RequestMethod.POST)
    public Result bindBuildingUser(@RequestBody BuildingUser buildingUser) {
        List<String> users = new ArrayList<>();
        users.addAll(buildingUser.getRepairs());
        users.addAll(buildingUser.getManages());
        buildingService.bindBuildingUser(buildingUser.getBuildingId(), users);
        return new Result();
    }

    @ApiOperation("解绑 宿舍楼 工作人员")
    @RequestMapping(value = "/unbind/user", method = RequestMethod.POST)
    public Result unbindBuildingUser(@RequestBody BuildingUser buildingUser) {
        List<String> users = buildingUser.getRepairs();
        users.addAll(buildingUser.getManages());
        buildingService.unbindBuildingUser(buildingUser.getBuildingId(), users);
        return new Result();
    }

    @ApiOperation("查询当前用户管理的宿舍楼")
    @RequestMapping(value = "/select/user", method = RequestMethod.GET)
    public Result selectBuildingByCurrentUserId() {
        String userId = ShiroUtils.getLoginUser().getUkId();
        BuildingDTO building = buildingService.selectBuildingByCurrentUserId(userId);
        return Result.response().data("building", building);
    }

}
